package chatNI;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.*;
import common.Message;

/**
 * UDPListen:
 * Classe contains the listen UDP socket.
 * @author bazile gaye
 */


public class UDPListen extends Thread{

    /**
     * Port number of udp socket. 
     * This number is common to everybody in the project.
     */
    public final static int portUDPlisten = 6666;

    private ChatNI ni;
    private int numPort;
    private Thread thread;
    private DatagramSocket sock;
    private DatagramPacket paquet;
    private Boolean threadActif = true ;

    /**
     * Contructor simple.
     */
    public UDPListen(){	}


    /**
     * Constructor.
     * @param chat network interface chatNI
     * @throws SocketException  if the socket can't be create.
     */
    public UDPListen(ChatNI chat) throws SocketException{
            this.numPort = portUDPlisten;
            this.ni = chat;
            this.sock = new DatagramSocket(this.numPort);
            this.thread = new Thread();
            thread.start();

    }

    /**
     * 
     * Socket udp in the state receive, it waits a datagram packet listenning the network.
     */
    public void run(){

            int size = 1000;
            byte[] buff = new byte[size];
            this.paquet = new DatagramPacket(buff,size);

            while(threadActif){
                    try {
                            sock.receive(paquet);
                            
                            // Si on reçoit un pacquet, on le transmet au ChatNI
                            ni.receive(transmit(paquet),paquet.getAddress());
                            
                    } catch (IOException erreurPacketReception) {
                            erreurPacketReception.printStackTrace();
                            System.out.println("Erreur dans la réception du paquet UDPlisten");
                    }
            }
    }

    /**
     * transmit:
     * Function use to transform a DatagramPAcket to a Message.
     * @param DatagramPAcket udp packet received by the socket.
     * @return Message message received.
     */
    private Message transmit(DatagramPacket pack) throws IOException{
             Message mess = null;
            try {
             ByteArrayInputStream bais = new ByteArrayInputStream(pack.getData()); 
             ObjectInputStream ois = new ObjectInputStream(bais);
             Object mes;
             mes = ois.readObject();
             mess = (Message) mes;

            } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                    System.out.println("Error in translation packet->MEssage");
            }
            return mess;
    }
	
	/**
	 * interrupt:
	 * Function use to stop the thread and close the socket.
	 */
    public void interrupt() {
        thread.interrupt();
        threadActif = false;
        sock.close();
    }
    
    /**
     * Get the number of the udp port used.
     * @return the number of this port.
     */
    public int getPortUDPlisten(){
    	return this.portUDPlisten;
    }
	
}
